package com.example.emos.wx.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/** 2-6 利用Swagger搭建 REST API*/
/**在swagger中开启jwt*/
/**在swagger中开启jwt*/
/**在swagger中开启jwt*/
/**在swagger中开启jwt*/
/**在swagger中开启jwt*/
/**在swagger中开启jwt*/
/**在swagger中开启jwt*/
@Configuration/**告诉springboot这是配置信息*/
@EnableSwagger2/**支持 2-6 利用Swagger搭建 REST AP*/
public class SwaggerConfig {
    //封装成docket，返回给springboot，加注解
    @Bean
    public Docket createRestApi() {
//_______________________________________________________________________________________________________

        /*创建swagger的标题，docket=》swagger*/
        /**new docket*/
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        //swagger的信息对象
        ApiInfoBuilder builder = new ApiInfoBuilder();
        //给信息添加内容
        builder.title("EMOS在线办公系统");
        ApiInfo info = builder.build();
        //builder给info，info给docket——————三层关系
        docket.apiInfo(info);//页面标题的信息

//_______________________________________________________________________________________________________


        //哪些包里面的类的方法、对象可以在swagger中测试
        /*包，包里面描述有ApiOperation的才可以调用*/
        ApiSelectorBuilder selectorBuilder = docket.select();
        //全部包，全部Java类都在swagger中
        selectorBuilder.paths(PathSelectors.any());
        //方法中有特定的注解ApiOperation才在swagger
        selectorBuilder.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class));
        docket = selectorBuilder.build();


//_______________________________________________________________________________________________________
        /*swagger，知道，请求头，令牌参数*/
        /**
         * 下面的语句是开启swagger对JWT的支持，当用户用Swagger调用受JWT认证保护的方法，
         * 必须要先提交参数（例如令牌）
         */
        /**规定用户需要输入什么参数，存储用户必须提交的参数*/
        /**请求的令牌信息放在头部header中，名称叫token*/
        ApiKey apiKey = new ApiKey("token", "token", "header");
        List<ApiKey> apiKeyList = new ArrayList<>();
        apiKeyList.add(apiKey);
        //这样docket就知道是在头部header中发送令牌，而且是在header中的token部分
        docket.securitySchemes(apiKeyList);


//_______________________________________________________________________________________________________
        /**(下面设置了作用域)，*/
        /*认证对象，作用域，令牌，不断封装，swagger中使用jwt，而不是springboot中jwt*/
        //如果用户JWT认证通过，则在Swagger中全局有效

        AuthorizationScope scope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] scopes = {scope};
        //存储令牌和作用域

        SecurityReference reference = new SecurityReference("token", scopes);
        List refList = new ArrayList();
        refList.add(reference);
        SecurityContext context = SecurityContext.builder().securityReferences(refList).build();
        List cxtList = new ArrayList();
        cxtList.add(context);
        docket.securityContexts(cxtList);

        return docket;
    }
}